package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * ClassName:UserController
 * Package:com.itheima.controller
 * Description:
 * Date:2020/3/11 16:08
 * User: snow
 */
@Controller
@RequestMapping("/user")
@RolesAllowed("ADMIN")
public class UserController {
	@Autowired
	private UserInfoService userInfoService;


	@RequestMapping("/findAll.do")
	public ModelAndView findAll
			(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
	                            @RequestParam(name = "size", required = true,defaultValue = "14")Integer size) throws Exception {
		ModelAndView view = new ModelAndView();
		List<UserInfo> userInfoList = userInfoService.findAll(page,size);
		// PageInfo是一个分页bean
		PageInfo pageInfo = new PageInfo(userInfoList);
		view.addObject("pageInfo",pageInfo);
		view.setViewName("user-list");
		return view;
	}

	@RequestMapping("/save.do")
	public String save(UserInfo userInfo) throws Exception {
		userInfoService.save(userInfo);
		return "redirect:findAll.do";
	}

	@RequestMapping("/findById.do")
	public ModelAndView findById(String id) throws Exception {
		ModelAndView view = new ModelAndView();
		UserInfo userInfo = userInfoService.findById(id);
		view.addObject("user", userInfo);
		view.setViewName("user-show");
		return view;
	}

	/**
	 * 查询用户以及用户可以添加的角色
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findUserByIdAndAllRole.do")
	public ModelAndView findUserByIdAndAllRole(String id) throws Exception {
		// 1、根据用户id查询用户
		UserInfo userInfo = userInfoService.findById(id);
		// 2、根据用户id查询可添加角色
		List<Role> otherRole = userInfoService.findOtherRole(id);
		for (Role role : otherRole) {
			System.out.println(role);
		}
		ModelAndView view = new ModelAndView();
		view.addObject("user", userInfo);
		view.addObject("roleList", otherRole);
		view.setViewName("user-role-add");
		return view;
	}

	/**
	 * 用户添加角色
	 *
	 * @param userId
	 * @param roleIds
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addRoleToUser.do")
	public String addRoleToUser
	(@RequestParam(name = "userId", required = true) String userId,
	 @RequestParam(name = "ids", required = true) String[] roleIds) throws Exception {
		userInfoService.addRoleToUser(userId, roleIds);
		return "redirect:findAll.do";
	}

	/**
	 * 冻结用户
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/freeze.do")
	public String freeze(String id) throws Exception {
		userInfoService.freeze(id);
		return "redirect:findAll.do";
	}

	/**
	 * 解禁用户
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/open.do")
	public String open(String id) throws Exception {
		userInfoService.open(id);
		return "redirect:findAll.do";
	}



}
